package org.shop.controller.app.live;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.ClassInfo;
import org.shop.entity.PostInfo;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.AnswerInfoService;
import org.shop.service.ClassInfoService;
import org.shop.service.ForumService;
import org.shop.service.PraiseService;
import org.shop.util.CommonUtil;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ForumContrller {
	private static Logger logger = LogManager.getLogger(ForumContrller.class);
	
	@Autowired
	private ForumService forumService;
	@Autowired
	private AnswerInfoService answerInfoService;
	@Autowired
	private PraiseService praiseService;
	@Autowired
	private ClassInfoService classInfoService;
	
    /**
     * app论坛发帖
     * @param req
     * @return
     */
    @RequestMapping("app/addPostMessage")
    @ResponseBody
    public Map<String, Object> addPostMessage(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        try{
        	String boardId = req.getParameter("boardId");
            String content = req.getParameter("content");
            String contentImg = req.getParameter("contentImg");
            String textBlackList=CommonUtil.getPropertiesParam("textBlackList");
            for(String textBlack:textBlackList.split(",")){
                if(content.contains(textBlack)){
                    map.put("state", 2);
                    map.put("msg", "发布内容不合法");
                    return map;
                }
            }
            MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            PostInfo postInfo = new PostInfo();
            postInfo.setBoardId(Long.parseLong(boardId));
            postInfo.setContent(content);
            postInfo.setContentImg(contentImg);
            postInfo.setSender(ud.getUserId());
            forumService.insertSelective(postInfo);
            map.put("state", 1);
            map.put("msg", "发布成功");
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "发布失败");
        }
        return map;
    }
    
    /**
     * app获取论坛所有贴子列表
     * @param req
     * @return
     */
    @RequestMapping("app/getPostMessageList")
    @ResponseBody
    public List<Map<String, Object>> getPostMessageList(HttpServletRequest req){
    	SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        	ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
    	String boardId = req.getParameter("boardId");
    	String content = req.getParameter("content");
    	String classId = req.getParameter("classId");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("boardId", boardId);
    	params.put("content", content);
    	params.put("classId", classId);
    	if(ObjUtil.isNotNullAndEmpty(ud)) {
    		params.put("userId", ud.getUserId());
    	}
		params.put("currentPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
		params.put("pageSize", pageSize);
    	List<Map<String, Object>> postMessageList = forumService.getPostMessageList(params);
    	List<Map<String, Object>> postMessageList2 = new ArrayList<Map<String,Object>>();
    	for(int i=0;i<postMessageList.size();i++) {
    		Map<String, Object> map = postMessageList.get(i);
    		Map<String, Object> m = new HashMap<String, Object>();
    		m.put("targetId", map.get("commentsId"));
    		map.put("praiseCount", praiseService.getPraiseCount(m));
    		m.put("postId", map.get("commentsId"));
    		map.put("answerCount", answerInfoService.getAnswerCount(m));
    		postMessageList2.add(map);
    	}
        return postMessageList2;
    }
    
    /**
     * app查看帖子详情
     * @param req
     * @return
     */
    @RequestMapping("app/getPostDetails")
    public String getPostDetails(HttpServletRequest req){
    	SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        	ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
		String commentsId = req.getParameter("commentsId");
		//更新浏览量
		PostInfo postInfo = forumService.selectByPrimaryKey(Long.parseLong(commentsId));
		postInfo.setBrowseNum(postInfo.getBrowseNum() + 1);
		forumService.updateByPrimaryKeySelective(postInfo);
		
		//根据贴子id获取贴子详情
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("commentsId", commentsId);
    	if(ObjUtil.isNotNullAndEmpty(ud)) {
    		params.put("userId", ud.getUserId());
    		req.setAttribute("userInfo", ud);
    	}
    	Map<String, Object> postDetails = forumService.getPostDetails(params);
    	req.setAttribute("postDetails", postDetails);
        return "app/find/forumDetails";
    }
    
    /**
     * app获取论坛内容所有分类
     * @param req
     * @return
     */
    @RequestMapping("app/getAllClassInfoList")
    @ResponseBody
    public List<ClassInfo> getAllClassInfoList(HttpServletRequest req){
    	List<ClassInfo> classInfoList = classInfoService.getAllClassInfoList();
        return classInfoList;
    }
    
}
